###############################################################################
#
# file: UserLoginDialog.py
# author: Victor Bos
# 
# Copyright 2007--2011 Victor Bos
#
# This file is part of GREATOR.
#
#    GREATOR is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    GREATOR is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with GREATOR.  If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
import Tkinter

class UserLoginDialog(Tkinter.Toplevel):

    def __init__(self, master=None, **kw):

        self.set_user_attr("usernames", kw, [])
        self.set_user_attr("pwcheckfunc", kw, None)
        self.selected_username = Tkinter.StringVar()
        self.pwcheck = False

        Tkinter.Toplevel.__init__(self, master, kw)
        self.protocol('WM_DELETE_WINDOW', self.cancel_pressed)

        f1 = self.create_main_frame()
        if f1 is not None:
            f1.pack(side = Tkinter.TOP, expand=1, fill=Tkinter.X)

        f3 = self.create_ok_cancel_buttons()
        f3.pack(side=Tkinter.TOP, anchor=Tkinter.E)

        self.center_on_master(master)

    def center_on_master(self, master):
        if master is not None:
            x = (master.winfo_rootx() +
                 ((master.winfo_width() - self.winfo_reqwidth()) / 2.0))
            y = (master.winfo_rooty() +
                 ((master.winfo_height() - self.winfo_reqheight()) / 2.0))
            self.geometry("+%d+%d" % (x,y))

    def set_user_attr(self, attrname, kw, default):
        if attrname in kw:
            setattr(self, attrname, kw[attrname])
            del kw[attrname]
        else:
            setattr(self, attrname, default)

    def create_main_frame(self):
        f = Tkinter.Frame(self)

        f2 = self.create_name_list(f)
        f2.pack(side = Tkinter.LEFT, expand=1, fill=Tkinter.X)

        f3 = self.create_password_panel(f)
        f3.pack(side = Tkinter.LEFT, expand=1, fill=Tkinter.BOTH)
        
        return f

    def create_name_list(self, parent):
        f = Tkinter.Frame(parent, borderwidth=3, relief="groove")

        for n in self.usernames:
            b = Tkinter.Radiobutton(
                f, 
                text=n, 
                variable=self.selected_username, value=n)
            b.pack(side=Tkinter.TOP, anchor=Tkinter.W)
        if len(self.usernames)>0:
            self.selected_username.set(self.usernames[0])
        return f

    def create_password_panel(self, parent):
        f = Tkinter.Frame(parent, borderwidth=3, relief="groove")
        l = Tkinter.Label(f, text="Password")
        l.pack(side=Tkinter.LEFT, anchor=Tkinter.N)
        
        # change into password entry
        l = Tkinter.Label(f, text="pw value")
        l.pack(side=Tkinter.LEFT, anchor=Tkinter.N)
        return f


    def create_ok_cancel_buttons(self):
        f = Tkinter.Frame(self)
        
        self.ok_button = Tkinter.Button(
            f,
            text="Ok",
            command=self.ok_pressed)
        self.ok_button.pack(side=Tkinter.LEFT)

        self.cancel_button = Tkinter.Button(
            f,
            text="Cancel",
            command=self.cancel_pressed)
        self.cancel_button.pack(side=Tkinter.LEFT)
        return f


    def cancel_pressed(self):
        self.pwcheck = False
        self.quit()
        self.destroy()

    def ok_pressed(self):
        self.pwcheck = self.pwcheckfunc(
            self.selected_username.get(), "Russ")
        self.quit()
        self.destroy()

    def go(self):
        self.wait_visibility() # window needs to be visible for the grab
        self.grab_set()
        self.mainloop()
        return self.pwcheck

def check_pw(name, pw, names_pws):
    return (name, pw) in names_pws

if __name__ == "__main__":
    root = Tkinter.Tk()
    root.withdraw()

    ns = ["Germany", "France", "United Kingdom", "Russia", "USA" ]
    ns_pws = [ (s, s[:4]) for s in ns ]

    uld = UserLoginDialog(
        usernames = ns,
        pwcheckfunc=lambda n, p : check_pw(n, p, ns_pws)
        )
    uld.title("User login dialog")
    pw = uld.go()
    pwvalid = "no"
    if pw:
        pwvalid = "yes"
    print "Selected player: %s, password ok: %s" % (uld.selected_username.get(), pw)
        
